import { SET_APP_STATE } from '../../constants/ActionType';
import * as APIS from '../../constants/APIS';
import { fetchPOST, fetchGET } from '../../utils/index';
var path = require('path');

export const setState = (value) => { return dispatch => dispatch({ type: SET_APP_STATE, payload: value }) };

export const login = (formData) => {
    return dispatch => {
        if (formData.username && formData.password) {
            fetchPOST(APIS.IS_LOGIN, formData)
                .then((res) => {
                    if (res.status) {
                        location = "/#/home";
                        alert(res.message);
                    } else {
                        alert(res.message);
                    }
                })
        } else {
            alert("Username or Password can not be null");
        }

    }
}

export const register = (formData) => {
    return dispatch => {
        if (formData.username && formData.password && formData.passwordConfirm) {
            if (formData.password === formData.passwordConfirm) {
                fetchPOST(APIS.IS_REGISTER, formData)
                    .then((res) => {
                        if (res.status) {
                            location = "/#/home";
                            alert(res.message);
                        } else {
                            alert(res.message);
                        }
                    })
            } else {
                alert("Please confirm the same password!");
            }

        } else {
            alert("Username or Password can not be null");
        }

    }
}

